package com.wecon.dianping.controller.admin;

import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import com.wecon.dianping.common.*;
import com.wecon.dianping.model.Seller;
import com.wecon.dianping.request.PageQuery;
import com.wecon.dianping.request.SellerCreateReq;
import com.wecon.dianping.service.SellerService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.validation.BindingResult;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.servlet.ModelAndView;

import javax.servlet.http.HttpServletResponse;
import javax.validation.Valid;
import java.io.IOException;
import java.util.List;

/**
 * @author Weikang  Wang
 * @program: com.wecon.dianping.controller.admin
 * @description:
 * @date 2020/3/17 14:45
 */
@RestController
@RequestMapping("/admin/seller")
public class SellerController {

    @Autowired
    private SellerService sellerService;

    /**
     * 获取商家列表
     * @return /
     */
    @RequestMapping("/index")
    @AdminPermission
    public ModelAndView index(PageQuery pageQuery) {
        PageHelper.startPage(pageQuery.getPage(), pageQuery.getSize());
        List<Seller> sellerList = sellerService.selectAll();
        PageInfo<Seller> sellerPageInfo = new PageInfo<>(sellerList);

        ModelAndView modelAndView = new ModelAndView("/admin/seller/index.html");
        modelAndView.addObject("data", sellerPageInfo);
        modelAndView.addObject("CONTROLLER_NAME", "seller");
        modelAndView.addObject("ACTION_NAME", "index");

        return modelAndView;
    }

    @RequestMapping("/createPage")
    @AdminPermission
    public ModelAndView createPage() {
        ModelAndView modelAndView = new ModelAndView("/admin/seller/create.html");
        modelAndView.addObject("CONTROLLER_NAME", "seller");
        modelAndView.addObject("ACTION_NAME", "create");

        return modelAndView;
    }

    @RequestMapping(value = "create", method = {RequestMethod.POST})
    @AdminPermission
    public void create(@Valid SellerCreateReq sellerCreateReq, BindingResult bindingResult, HttpServletResponse response) throws BusinessException, IOException {
        if (bindingResult.hasErrors()) {
            throw new BusinessException(EmBusinessError.PARMETER_VALIDATION_ERROR, CommonUtil.processErrorString(bindingResult));
        }

        Seller seller = new Seller();
        seller.setName(sellerCreateReq.getName());
        sellerService.create(seller);

        response.sendRedirect("/admin/seller/index");
    }

    /**
     * 禁用操作
     * @param id 商户id
     * @return /
     */
    @RequestMapping(value = "down", method = {RequestMethod.POST})
    @AdminPermission
    @ResponseBody
    public CommonResult down(@RequestParam(value = "id")Integer id) throws BusinessException {
        Seller seller = sellerService.changeStatus(id, 1);
        return CommonResult.create(seller);
    }

    /**
     * 启用操作
     * @param id 商户id
     * @return /
     */
    @RequestMapping(value = "up", method = {RequestMethod.POST})
    @AdminPermission
    @ResponseBody
    public CommonResult up(@RequestParam(value = "id")Integer id) throws BusinessException {
        Seller seller = sellerService.changeStatus(id, 0);
        return CommonResult.create(seller);
    }

}
